package com.hezuti.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

public class PropertyReader {
    private Properties       properties;
    private FileInputStream  inputFile;
    private FileOutputStream outputFile;

    public PropertyReader() {
	this.properties = new Properties();
    }

    public PropertyReader(String filePath) {
	this.properties = new Properties();
	try {
	    this.properties.load(getClass().getResourceAsStream(filePath));
	} catch (FileNotFoundException ex) {
	    System.out.println("读取属�?文件--->失败�? 原因：文件路径错误或者文件不存在");
	    ex.printStackTrace();
	} catch (IOException ex) {
	    System.out.println("装载文件--->失败!");
	    ex.printStackTrace();
	}
    }

    public String getValue(String key) {
	if (this.properties.containsKey(key)) {
	    String value = this.properties.getProperty(key);
	    return value;
	}
	return "";
    }

    public String getValue(String fileName, String key) {
	String value;
	try {
	    value = "";
	    this.inputFile = new FileInputStream(fileName);
	    this.properties.load(this.inputFile);
	    this.inputFile.close();
	    if (this.properties.containsKey(key)) {
		value = this.properties.getProperty(key);
		return value;
	    }
	    return value;
	} catch (FileNotFoundException e) {
	    e.printStackTrace();
	    return "";
	} catch (IOException e) {
	    e.printStackTrace();
	    return "";
	} catch (Exception ex) {
	    ex.printStackTrace();
	}
	return "";
    }

    public void clear() {
	this.properties.clear();
    }

    public void setValue(String key, String value) {
	this.properties.setProperty(key, value);

    }

    public void saveFile(String fileName, String description) {
	try {
	    this.outputFile = new FileOutputStream(fileName);
	    this.properties.store(this.outputFile, description);
	    this.outputFile.close();
	} catch (FileNotFoundException e) {
	    e.printStackTrace();
	} catch (IOException ioe) {
	    ioe.printStackTrace();
	}
    }
}